<?php
defined('BY_JYA') or exit('error');

$return=array();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('core_account_product_fenlei',array('uniacid'=>$_SESSION['uniacid']),array('id','title','img'));
	$return['url']=custom_url('core/welcome.home/',true);
	if($fenlei){
		foreach($fenlei as &$item){
			$item['url']=custom_url('core/product.list//group_id='.$item['id'],true);
			$item['img'] = $item['img'] ? $item['img'] : '../core/resource/images/nopic.jpg';
		}
	}
	$return['item']['fenleiurl']=custom_url('core/product.list//group_id='.$return['item']['group_id'],true);
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['group_id']){
		$params['group_id']=$_RQ['group_id'];
	}
	$fields=array('id','title','group_id','createtime','readnum','biaoqian','status','desc','price','qrcode','content','picurl');
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('core_account_product',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['content'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);
			$item['picurl'] = $item['pic_url'] ? $item['pic_url'] : '../core/resource/images/nopic.jpg';
			$item['createtime']=date('y/m/d H:i:s',$item['createtime']);
			$item['group_id']=$return['fenlei'][$item['group_id']]['title'];
			$item['url']=custom_url('core/product.list/detail/id='.$item['id'],true);
			$biaoqian=explode(';',$item['biaoqian']);
			$bqs=pdo_get('core_account_product_biaoqian',array('uniacid'=>$_SESSION['uniacid']),array('bqs'));
			$bqs=explode(' ',$bqs['bqs']);
			$item['biaoqian']=array();
			foreach($biaoqian as $key => $value){
				if($value != ''){
					$item['biaoqian'][$key] = $bqs[$value];
				}
			}
			
		}
	}
	$group_id = intval($_RQ['group_id']);
	$return['sharedata'] = array(
		'title'=>$_RQ['group_id'] ? $return['fenlei'][$group_id]['title'] . '-' . $_SESSION['account']['title']: '文章分类 -'.$_SESSION['account']['title'],
	);
	$return['fenleiname']=$fenlei[$fenleiid]['title'];
	$return['fenlei']=$fenlei?$fenlei:array();
	$return['account']=$_SESSION['account'];
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['list']=$list?$list:array();
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['extradata']=array();
	exi($return);
}
if($_RQ['op']=='detail'){
	$id = intval($_RQ['id']);
	$_SESSION['id'] = $id;
	//刷新阅读次数
	pdo_update('core_account_product',array('readnum +='=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	//获取热门
	$pindex = max(1, intval($_RQ['page']));
	$psize = 6;
	$total='';
	$fields=array('id','title','group_id','createtime','readnum','tuijian','biaoqian','status','pingfen','price','qrcode','picurl');
	$return['hots']=pdo_getslice('core_account_product',array('uniacid'=>$_SESSION['uniacid'],'tuijian '=>1),array($pindex,$psize),$total,$fields,'','id desc');
	$return['good']=pdo_getslice('core_account_product',array('uniacid'=>$_SESSION['uniacid']),array($pindex,$psize),$total,$fields,'','pingfen desc');
	//获取文章数据
	$return['item']=pdo_get('core_account_product',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','picurl','group_id','createtime','biaoqian','slide','readnum','status','desc','price','qrcode','content'));
	$return['item']['pingfenlog']=$return['item']['pingfenlog']?json_decode($return['item']['pingfenlog'],true):array();
	$return['item']['pingfennum5'] = 0;
	$return['item']['pingfennum4'] = 0;
	$return['item']['pingfennum3'] = 0;
	$return['item']['pingfennum2'] = 0;
	$return['item']['pingfennum1'] = 0;
	$pingfenloglen  = count($return['item']['pingfenlog']);
	foreach($return['item']['pingfenlog'] as $item){
		if($item['fanshu'] == 5){
			$return['item']['pingfennum5'] +=1;
		}
		if($item['fanshu'] == 4){
			$return['item']['pingfennum4'] +=1;
		}
		if($item['fanshu'] == 3){
			$return['item']['pingfennum3'] +=1;
		}
		if($item['fanshu'] == 2){
			$return['item']['pingfennum2'] +=1;
		}
		if($item['fanshu'] == 1){
			$return['item']['pingfennum1'] +=1;
		}
	}
	if($pingfenloglen){
		$return['item']['pingfennum5_bfb'] = ($return['item']['pingfennum5'] / $pingfenloglen) * 100;
		$return['item']['pingfennum4_bfb'] = ($return['item']['pingfennum4'] / $pingfenloglen) * 100;
		$return['item']['pingfennum3_bfb'] = ($return['item']['pingfennum3'] / $pingfenloglen) * 100;
		$return['item']['pingfennum2_bfb'] = ($return['item']['pingfennum2'] / $pingfenloglen) * 100;
		$return['item']['pingfennum1_bfb'] = ($return['item']['pingfennum1'] / $pingfenloglen) * 100;
	}
	//获取标签
	$biaoqian=explode(';',$return['item']['biaoqian']);
	$bqs=pdo_get('core_account_product_biaoqian',array('uniacid'=>$_SESSION['uniacid']),array('bqs'));
	$bqs=explode(' ',$bqs['bqs']);
	$return['biaoqian']=array();
	foreach($biaoqian as $key => $value){
		if($value != ''){
			$return['biaoqian'][$key] = $bqs[$value];
		}
	}
	unset($return['item']['readnum']);
	$return['item']['createtime']=date('20y-m-d',$return['item']['createtime']);
	$return['fenlei']=pdo_getall('core_account_product_fenlei',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['item']['fenleiurl']=custom_url('core/product.list//id='.$return['item']['group_id'],true);
	$return['item']['fenlei']=$return['fenlei'][$return['item']['group_id']]['title'];
	$return['item']['slide']=json_decode($return['item']['slide'],true);
	//$return['item']['content'] = str_replace('&nbsp;','',$return['item']['content']);
	$return['item']['desc'] = $return['item']['desc'] ? $return['item']['desc'] : cfc('tools')->substr($return['item']['content'],0,36);
	$return['sharedata'] = array(
		'title'=>$return['item']['title'] . '-' . $_SESSION['account']['title'],
		'desc'=>$return['item']['desc'],
		'picurl'=>$return['item']['picurl'],
	);
	$return['account']=$_SESSION['account'];
	set_html('title',$return['item']['title']).'-'.$return['account']['name'];
	$return['extradata']=array();
	exi($return);
}
if($_RQ['op']=='content'){
	$content=pdo_getcolumn('core_account_product',array('id'=>$_SESSION['id'],'uniacid'=>$_SESSION['uniacid']),'content');
	if(!$content){
		exit('找不到内容！');
	}
	echo $content;
	exit;
}
if($_RQ['op']=='pingfen'){
	$pingfenlog=pdo_getcolumn('core_account_product',array('id'=>$_SESSION['id'],'uniacid'=>$_SESSION['uniacid']),'pingfenlog');
	$pingfenlog=$pingfenlog?json_decode($pingfenlog,true):array();
	if(empty($_RQ['fenshu'])){
		$return['message'] = '没选择分数？';
	}elseif(empty($_SESSION['uid'])){
		$return['message'] = '登陆后才能评分哦！';
	}else{
		$pingfen = array('uid'=>$_SESSION['uid'],'fenshu'=>$_RQ['fenshu']);
		$pingfenlog[] = $pingfen;
		$pingfenlog = ijson_encode($pingfenlog);
		pdo_update('core_account_product',array('pingfenlog'=>$pingfenlog),array('id'=>$_SESSION['id'],'uniacid'=>$_SESSION['uniacid']));
		$return['success']=1;
	}
	exi($return);
}